package indi.cyh.fastservice.base;

import indi.cyh.fastservice.base.entity.http.ResponseBase;
import indi.cyh.fastservice.base.request.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import indi.cyh.fastservice.tool.RestResultTool;

/**
 * @Description TODO
 * @Author CYH
 * @Date 2021/7/20 18:35
 **/
public abstract class BaseControllerImpl<T extends BaseBiz> implements BaseController {

    @Autowired
    protected T biz;

    @Autowired
    protected RestResultTool resultTool;

    @ApiOperation(value = "保存数据")
    @Override
    @PutMapping("/save")
    public ResponseBase save(@RequestBody @Validated BaseSaveRequest request) {
        try {
            return resultTool.getOverResult(biz.save(request), "保存失败!", "保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return resultTool.getExceptionResult("", "", e);
        }
    }

    @ApiOperation(value = "删除数据")
    @Override
    @DeleteMapping("/delete")
    public ResponseBase delete(@Validated BaseDeleteRequest request) {
        try {
            return resultTool.getOverResult(biz.delete(request), "保存失败!", "删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return resultTool.getExceptionResult("", "", e);
        }
    }

    @ApiOperation(value = "查询分页数据")
    @Override
    @GetMapping("/queryPageData")
    public ResponseBase queryPageData(@Validated BaseQueryPageDataRequest request) {
        try {
            return resultTool.getOverResult(biz.queryPageData(request), "查询失败!", "查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return resultTool.getExceptionResult("", "", e);
        }
    }

    @ApiOperation(value = "查询一条数据")
    @Override
    @GetMapping("/queryOne")
    public ResponseBase queryOne(@Validated BaseQueryOneRequest request) {
        try {
            return resultTool.getOverResult(biz.queryOne(request), "查询失败!", "查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return resultTool.getExceptionResult("", "", e);
        }
    }

    @ApiOperation(value = "查询列表")
    @Override
    @GetMapping("/queryList")
    public ResponseBase queryList(@Validated BaseQueryListRequest request) {
        try {
            return resultTool.getOverResult(biz.queryList(request), "查询失败!", "查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return resultTool.getExceptionResult("", "", e);
        }
    }
}
